<template>
    <div>
        <!-- 头部区域 -->
        <van-nav-bar :title="`${comment.reply_count}条回复`" class="nav-bar">
            <van-icon slot="right" name="cross" @click="$emit('close')" />
        </van-nav-bar>

        <!-- 评论内容 -->
        <comment-item :comments="comment"></comment-item>

        <van-cell>所有回复</van-cell>

        <!-- 回复评论列表 -->
        <comment-list
            type="c"
            :source="comment.com_id"
            :list="commentList"
        ></comment-list>

        <!-- 文章底部 -->
        <div class="article-bottom">
            <van-button
                class="comment-btn"
                round
                type="default"
                size="small"
                @click="isPostShow = true"
                >写评论</van-button
            >
        </div>

        <!-- 发布回复弹出层 -->
        <van-popup v-model="isPostShow" position="bottom">
            <post-comment
                :articleId="articleId"
                :target="comment.com_id"
                @post-success="onPostSuccess"
            ></post-comment>
        </van-popup>
    </div>
</template>

<script>
import CommentItem from './comment-item'
import CommentList from './comment-list'
import PostComment from './post-comment'

export default {
    components: {
        CommentItem,
        CommentList,
        PostComment
    },
    props: {
        comment: {
            type: Object,
            required: true
        },
        articleId: {
            type: [String, Number, Object],
            required: true
        }
    },
    data () {
        return {
            // 控制发布评论弹出层是否显示
            isPostShow: false,
            // 评论的回复列表
            commentList: []
        }
    },
    created () { },
    computed: {},
    methods: {
        // 发布评论
        onPostSuccess (data) {
            // 把发布成功的评论数据对象放到评论列表顶部
            this.commentList.unshift(data)

            // 发布成功后评论数量加一
            this.comment.reply_count++

            this.isPostShow = false
        }
    },
    watch: {}
}
</script>

<style scoped>
.nav-bar {
    background-color: #fff;
}
.nav-bar /deep/ .van-nav-bar__title {
    color: #333;
}
.article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 44px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
}
.comment-btn {
    width: 200px;
    height: 30px;
    border: 1px solid #eee;
    font-size: 15px;
    line-height: 23px;
    color: #a7a7a7;
}
</style>
